package logging;

import java.io.File;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;






import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import Filter.CustomFilter;
import Filter.DateFilter;
import Filter.Filter;
import Filter.LevelFilter;
import Filter.TermFilter;
import Filter.RegularExpressionFilter;


public class XmlConfig extends Config{
	
	
	public XmlConfig(String path){
		this.path = path;
		this.enableConsole=false;
		this.separator="-";
		this.hasConsoleFilter=false;
		this.filterConsole=null;
		this.pathValid=(fileExist() && path.contains(".xml"));
	}
	
	public void readOneElement(Element elementOne)  throws Exception{
		
		   this.formatConsole= elementOne.getChildText("formatConsole");
		   this.formatFiles= elementOne.getChildText("formatFiles");
		   this.separator=elementOne.getChildText("separator");
		   this.level=elementOne.getChildText("levelDefault");

			if (elementOne.getChildText("console").equals("yes"))
				this.enableConsole=true;	

//			this.typeFormatConsole=elementOne.getChildText("typeFormatConsole");
			this.typeFormat=elementOne.getChildText("typeFormat");
		
			this.consoleTypeFilter= elementOne.getChildText("consoleTypeFilter");
			
			if (consoleTypeFilter.equals("CUSTOM")){
				String consoleNameClase=elementOne.getChildText("consoleNameClase");
				String consoleListParameters=elementOne.getChildText("consoleListParameters");		

				ArrayList<String> listParameters = new ArrayList<String>();
				if (consoleListParameters != null && consoleListParameters != "")
					listParameters=this.slipParameters(consoleListParameters);
				
				Object[] arg= null;

				if (listParameters.size() > 0){
					arg = new Object[listParameters.size()];
					for (int i=0; i<listParameters.size(); i++){
						arg [i] =  listParameters.get(i);
					}
				}else{
					arg = new Object[0];
				}
											
				this.filterConsole = new Filter (new CustomFilter(consoleNameClase, arg));	

			}
			
			else{
				this.consoleValueFilter= elementOne.getChildText("consoleValueFilter"); 
		   
				String valueFilter, typeFilter;
				typeFilter=this.consoleTypeFilter;
				valueFilter=this.consoleValueFilter;
				if (typeFilter.equals("TERM")){
					this.filterConsole = new Filter (new TermFilter(valueFilter));				
				}
				else
					if (typeFilter.equals("LEVEL")) {										
						this.filterConsole = new Filter (new LevelFilter(valueFilter));					
					}
					else
						if (typeFilter.equals("DATE")) {										
							this.filterConsole = new Filter (new DateFilter(valueFilter));
						}
						else
							if (typeFilter.equals("REGULAREXPRESSION")) {										
								this.filterConsole = new Filter (new RegularExpressionFilter(valueFilter));
							}						
							else									
								this.hasConsoleFilter=false;				
			}			
	}
	
	private void loadFilterCustom(Element element, String file) throws Exception {
		
		String nameClase=element.getChildText("fileNameClase");
		String listParametersValues=element.getChildText("listParametersFilter");

		ArrayList<String> listParameters = new ArrayList<String>();
		if (listParametersValues != null && listParametersValues != "")
			listParameters=this.slipParameters(listParametersValues);
		
		Object[] arg= null;

		if (listParameters.size() > 0){
			arg = new Object[listParameters.size()];
			for (int i=0; i<listParameters.size(); i++){
				arg [i] =  listParameters.get(i);
			}
		}else{
			arg = new Object[0];
		}
					
		Filter filter = new Filter (new CustomFilter(nameClase.trim(), arg));		
		this.files.put(file, filter);	
		
	}
	
	private  void readNextElement(Element element){

				String valueFilter, file, typeFilter, nameClase, parameters;
				Filter filter;
			
					file=element.getChildText("path");
					
					if (file.equalsIgnoreCase("DestinoCustom")){
						nameClase=element.getChildText("nameClase");
						parameters=element.getChildText("listParameters");
						ArrayList<String> listParameters = new ArrayList<String>();
						if (parameters != null && parameters != "")
							listParameters=this.slipParameters(parameters);
						this.destinationCustom.put(nameClase,listParameters);				
						file=nameClase;

					}
					
					
					
					
					typeFilter=element.getChildText("typeFilter");

					if (typeFilter.equals("CUSTOM")) 	{	
						try {
							loadFilterCustom(element, file);
						} catch (Exception e) {

							e.printStackTrace();
						}
					}
					else {
					valueFilter=element.getChildText("valueFilter");
					if (typeFilter.equals("TERM")){
						filter = new Filter (new TermFilter(valueFilter));
						this.files.put(file, filter);					
					}
					else
						if (typeFilter.equals("LEVEL")) {											
							filter = new Filter (new LevelFilter(valueFilter));	
							this.files.put(file, filter);					
						}
						else
							if (typeFilter.equals("DATE")) {											
								filter = new Filter (new DateFilter(valueFilter));	
								this.files.put(file, filter);					
							}
							else
								if (typeFilter.equals("REGULAREXPRESSION")) {											
									filter = new Filter (new RegularExpressionFilter(valueFilter));	
									this.files.put(file, filter);					
								}										
							else						
								this.files.put(file, null);							     
				}							
}
			
	
	
 public void readConfig(){   
         
         //creating JDOM SAX parser
         SAXBuilder builder = new SAXBuilder();
       
         //reading XML document
         Document xml = null;
         try {
                 xml = builder.build(new File(path));
         } catch (JDOMException e) {
        	 this.pathValid=false;
             e.printStackTrace();
         } catch (IOException e) {
        	 this.pathValid=false;
              e.printStackTrace();
         }
       
         //getting root element from XML document
         Element root = xml.getRootElement();
                
         List<Element> elements = root.getChildren();
       
         //Iterating over all childs in XML
         Iterator<Element> itr = elements.iterator();
         Element elementOne = itr.next();
         
         //read the one element to xml
		try {
			readOneElement(elementOne);
			} catch (Exception e) {

			e.printStackTrace();
			}
     
         
         while(itr.hasNext()){
                 Element element = itr.next();

                 //reading value of childern in XML using JDOM
                 readNextElement(element);
         }
 }
 
 
 
}
